package com.hzjx.fenci.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hzjx.fenci.model.SingleWord;
import com.hzjx.fenci.service.SingleWordService;

@Controller
public class SingleWordControler {
	                        
	private Logger log = Logger.getLogger(this.getClass());
	

	private SingleWordService singleWordService;
	
	@Autowired
	public SingleWordControler(SingleWordService singleWordService){
		this.singleWordService=singleWordService;
	}
	
	
	@RequestMapping("/listSingleWord.do")
	public ModelAndView listSingleWord(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<SingleWord> singleWordList =this.singleWordService.getAll();
		ModelAndView mv = new ModelAndView();
		mv.addObject("singleWordList",singleWordList);
		mv.setViewName("listSingleWord");
		return mv;
	}
	
	@RequestMapping("/editSingleWord.do")
	public ModelAndView editSingleWord(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		String id=request.getParameter("id");
		SingleWord sw=null;
		if(id!=null){
			sw=this.singleWordService.getObject(id);
		}else{
			sw =new SingleWord();
		}
		
		
		
		ModelAndView mv = new ModelAndView();
		
		mv.addObject(sw);
		mv.setViewName("editSingleWord");
		return mv;
		
	}
	
	

	
	@RequestMapping("/saveSingleWord.do")
	public String saveSingleWord(@ModelAttribute("singleWord") SingleWord singleWord, BindingResult result) {

		this.singleWordService.update(singleWord);
		//status.setComplete();
		return "redirect:/listSingleWord.do";
	}
	
	
	@RequestMapping("/deleteSingleWord.do")
	public String deleteSingleWord(HttpServletRequest request,HttpServletResponse response)throws Exception {
		String id=request.getParameter("id");
		this.singleWordService.del(id);
		return "redirect:/listSingleWord.do";
	}
	

}
